# 公共的工具文件
from flask import session,current_app,g
from info.models import User
from functools import wraps

def do_rank(index):
    '''根据index,返回对应first，second,third'''
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return  'third'
    else:
        return  ''


def user_login_data(view_func):
    '''使用装饰器'''
    @wraps(view_func)
    def wrapper(*agrs,**kwargs):
        user_id = session.get('user_id', None)
        user = None
        if user_id:
            # 表示用户已经登录，然后查询用户的信息
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user =user
        return view_func(*agrs,**kwargs,)
    return wrapper














# def user_login_data():
#     user_id = session.get('user_id', None)
#     user = None
#     if user_id:
#         # 表示用户已经登录，然后查询用户的信息
#         try:
#             user = User.query.get(user_id)
#         except Exception as e:
#             current_app.logger.error(e)
#     return user